/**
 * Created by huotaol on 2017/5/11.
 */
import React, {Component} from "react";
import PropTypes from "prop-types";
import {ListView, View} from "react-native";
import CustomStyle from "../../../common/style/CustomStyle";
import ModuleCommonHeader from "../../main/module/ModuleCommonHeader";
import * as CommonUtil from "../../../common/util/Util";
import CheckDetailItem from "./component/CheckDetailItem";

const DATA_SOURCE = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});

export default class CheckParamDetail extends Component {
    static propTypes = {
        imagePrefix: PropTypes.string,
        title: PropTypes.string,
        attrs: PropTypes.object,
    };

    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.state = {list: []};
        this._renderRow = this._renderRow.bind(this);
    }

    componentDidMount() {
        const data = [];
        for (let [key, value] of Object.entries(this.props.attrs)) {
            const param = {};
            param.name = value[1];
            const extraData = value[0];
            param.normal = parseInt(extraData) === 0;
            if (CommonUtil.isArray(extraData)) {
                param.extraList = extraData.map(item => {
                    let [title, uri] = item.split('#');
                    return {title: title, uri: this.props.imagePrefix + uri};
                });
            } else {
                param.extraStr = param.normal ? '' : extraData;
            }
            data.push(param);
        }
        this.setState({list: data});
    }

    render() {
        return (
            <View style={{flex: 1}}>
                <ModuleCommonHeader title={this.props.title}/>
                <ListView
                    dataSource={DATA_SOURCE.cloneWithRows(this.state.list)}
                    renderSeparator={this._renderSeparator}
                    renderRow={this._renderRow}/>
            </View>
        );
    }


    _renderRow(data) {
        return <CheckDetailItem title={data.name} normal={data.normal} extraStr={data.extraStr}
                                extraList={data.extraList}/>;
    }


    _renderSeparator() {
        return <View style={[CustomStyle.separator]}>
        </View>
    }
}
